package com.bq.gametrade.ui.activity;

import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bq.gametrade.HttpLink;
import com.bq.gametrade.R;
import com.bq.gametrade.adapter.RedEnvelopeRecordAdapter;
import com.bq.gametrade.base.BaseActivity;
import com.bq.gametrade.bean.RedEnvelopeRecords;
import com.bq.gametrade.bean.RedEnvelopeRecordsDetails;
import com.bq.gametrade.bean.UserInfoDetails;
import com.bq.gametrade.http.VRHttp;
import com.bq.gametrade.http.VRHttpListener;
import com.bq.gametrade.utils.Base64Util;
import com.bq.gametrade.utils.DateUtils;
import com.bq.gametrade.utils.ToastUtils;
import com.bq.gametrade.view.pullableview.PullToRefreshLayout;
import com.bq.gametrade.view.pullableview.PullableListView;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

public class RedEnvelopeRecordsActivity extends BaseActivity implements View.OnClickListener {

    private ImageView back;
    private TextView btnMySendRedEnvelope, btnReceiveRedEnvelope;
    private String redEnvelopeType = "1";//0:自己发的 1：自己接收的
    private PullToRefreshLayout pullToRefreshLayout;
    private PullableListView listView;
    private LinearLayout layPrompt;
    private ArrayList<RedEnvelopeRecordsDetails> mList = new ArrayList<>();
    private RedEnvelopeRecordAdapter adapter;
    private int pageIndex = 2;
    private UserInfoDetails userInfo;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            Window window = getWindow();
            window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
            window.setStatusBarColor(getResources().getColor(R.color.redEnvelope_bg));

        }
        setContentView(R.layout.activity_redenvelope_records);
        if(application.isLogin()){
            userInfo = application.getUserInfo();
        }else {
            startActivity(new Intent(this, LoginActivity.class));
            finish();
        }
        initView();
        initListView();
        getRedEnvelopeRecords(redEnvelopeType);
    }

    private void initView() {
        back = serchViewById(R.id.image_back);
        back.setOnClickListener(this);
        btnReceiveRedEnvelope = serchViewById(R.id.btn_receiveRedEnvelope);
        btnReceiveRedEnvelope.setOnClickListener(this);
        btnMySendRedEnvelope = serchViewById(R.id.btn_mySendRedEnvelope);
        btnMySendRedEnvelope.setOnClickListener(this);
        layPrompt = serchViewById(R.id.lay_prompt);

    }

    private void initListView() {
        pullToRefreshLayout = serchViewById(R.id.layout_refresh);
        listView = serchViewById(R.id.lisView_redEnvelopeRecords);
        adapter = new RedEnvelopeRecordAdapter(RedEnvelopeRecordsActivity.this, mList, application);
        listView.setAdapter(adapter);
        pullToRefreshLayout.setOnRefreshListener(new PullToRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh(final PullToRefreshLayout pullToRefreshLayout) {

                if (!application.isLogin()) {
                    pullToRefreshLayout.loadmoreFinish(PullToRefreshLayout.FAIL);
                    toastShow("您还没有登陆");
                    return;
                }
                getRedEnvelopeRecords(redEnvelopeType);
                pullToRefreshLayout.loadmoreFinish(PullToRefreshLayout.SUCCEED);
            }

            @Override
            public void onLoadMore(final PullToRefreshLayout pullToRefreshLayout) {
                // 加载操作
                getRedEnvelopeRecordsResfresh(redEnvelopeType, pullToRefreshLayout);
            }
        });
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.image_back:
                finish();
                break;
            case R.id.btn_receiveRedEnvelope:
                btnReceiveRedEnvelopePress();
                redEnvelopeType = "1";
                getRedEnvelopeRecords("1");
                pageIndex = 2;
                break;
            case R.id.btn_mySendRedEnvelope:
                btnMySendRedEnvelopePress();
                redEnvelopeType = "0";
                getRedEnvelopeRecords("0");
                pageIndex = 2;
                break;
        }
    }

    private void btnReceiveRedEnvelopePress() {
        btnReceiveRedEnvelope.setBackground(getResources().getDrawable(R.drawable.bg_redenvelope_btn_left_press));
        btnReceiveRedEnvelope.setTextColor(getResources().getColor(R.color.redEnvelope_bg));
        btnMySendRedEnvelope.setBackground(getResources().getDrawable(R.drawable.bg_redenvelope_btn_right_normal));
        btnMySendRedEnvelope.setTextColor(getResources().getColor(R.color.white));
    }

    private void btnMySendRedEnvelopePress() {
        btnReceiveRedEnvelope.setBackground(getResources().getDrawable(R.drawable.bg_redenvelope_btn_left_normal));
        btnReceiveRedEnvelope.setTextColor(getResources().getColor(R.color.white));
        btnMySendRedEnvelope.setBackground(getResources().getDrawable(R.drawable.bg_redenvelope_btn_right_press));
        btnMySendRedEnvelope.setTextColor(getResources().getColor(R.color.redEnvelope_bg));
    }

    private void getRedEnvelopeRecords(String type) {
        mList.clear();
        Map<String, String> params = new HashMap<>();
        params.put("token", userInfo.token);
        params.put("curPage", "1");
        params.put("type", type);
        showSpotsDialog("正在加载···");
        VRHttp.getRedEnvelopeRecords(RedEnvelopeRecordsActivity.this, HttpLink.RED_ENVELOPE_RECORDS_URL, params, new VRHttpListener() {
            @Override
            public void onSuccess(Object response, boolean isCache) {
                if (response != null) {
                    RedEnvelopeRecords records = (RedEnvelopeRecords) response;
                    if (records.status.equals("0")) {
                        mList = records.luck_list;
                        if (mList.size() > 0) {
                            adapter.setData(mList);
                            adapter.notifyDataSetChanged();
                            layPrompt.setVisibility(View.GONE);
                        } else {
                            layPrompt.setVisibility(View.VISIBLE);

                        }
                    } else if (records.status.equals("99")) {
                        toastShow("登录超时，重新登录");
                        application.setUserInfo(null);
                        startActivity(new Intent(RedEnvelopeRecordsActivity.this, LoginActivity.class));
                    } else if (records.status.equals("98")) {
                        toastShow("您的账号已在其他设备登录，如果不人本人登录请及时修改密码");
                        application.setUserInfo(null);
                        startActivity(new Intent(RedEnvelopeRecordsActivity.this, LoginActivity.class));
                    }
                } else {
                    pullToRefreshLayout.refreshFinish(PullToRefreshLayout.FAIL);
                }
            }


        @Override
        public void onFinish () {
            super.onFinish();
            closeSpotsDialog();
        }
    }

    );
}

    private void getRedEnvelopeRecordsResfresh(String type, final PullToRefreshLayout pullToRefreshLayout) {
        mList.clear();
        Map<String, String> params = new HashMap<>();
        params.put("token", userInfo.token);
        params.put("curPage", String.valueOf(pageIndex));
        params.put("type", type);
        VRHttp.getRedEnvelopeRecords(RedEnvelopeRecordsActivity.this, HttpLink.RED_ENVELOPE_RECORDS_URL, params, new VRHttpListener() {
            @Override
            public void onSuccess(Object response, boolean isCache) {
                if (response != null) {
                    RedEnvelopeRecords records = (RedEnvelopeRecords) response;
                    if (records.status.equals("0")) {
                        pullToRefreshLayout.refreshFinish(PullToRefreshLayout.SUCCEED);
                        mList = records.luck_list;
                        if (mList.size() > 0) {
                            adapter.setData(mList);
                            adapter.notifyDataSetChanged();
                        } else {
                            toastShow("没有更多数据了");
                            pullToRefreshLayout.refreshFinish(PullToRefreshLayout.SUCCEED);
                        }
                    } else if (records.status.equals("99")) {
                        toastShow("登录超时，重新登录");
                        application.setUserInfo(null);
                        startActivity(new Intent(RedEnvelopeRecordsActivity.this, LoginActivity.class));
                    } else if (records.status.equals("98")) {
                        toastShow("您的账号已在其他设备登录，如果不人本人登录请及时修改密码");
                        application.setUserInfo(null);
                        startActivity(new Intent(RedEnvelopeRecordsActivity.this, LoginActivity.class));
                    }
                } else {
                    pullToRefreshLayout.refreshFinish(PullToRefreshLayout.FAIL);
                }
            }
        });
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        pageIndex = 2;
    }
}
